<?php
// +----------------------------------------------------------------------
// | When work is a pleasure, life is a joy!
// +----------------------------------------------------------------------
// | User: ShouKun Liu  |  Email:24147287@qq.com  | Time:2017/3/26 13:24
// +----------------------------------------------------------------------
// | TITLE: 业务基础类
// +----------------------------------------------------------------------
namespace app\first\controller;

use app\common\controller\Jwt;
use app\first\model\Member as Member;
use DawnApi\facade\ApiController;
use DawnApi\facade\Send;
use think\facade\Config;
use think\facade\Lang;

class Base extends ApiController{
    protected static $uid = 0;
    protected static $user = [];
    protected $site= null;
    /**
     * 允许访问的请求类型
     * @var string
     */
    public $restMethodList = 'get|post|put|delete|patch|head|options';
    public $restActionList=['index','get','create','save','read','edit','update','delete','post'];
    //是否开启授权认证
    public $apiAuth = true;
    use Send;
    public function __construct(){
//        ini_set('memory_limit', '-1');
        parent::__construct();
        $this->site = db('config')->order('id desc')->find();
        if(self::$app){
            self::$uid = isset(self::$app['auth'])?self::$app['auth']->uid:0;
            self::$user = self::$app['auth']->member??null;
        }
        Lang::setAllowLangList(['zh-cn','en-us']);
    }
    /**
     * 获取密码
     * @param $pwd
     * @param int $type
     * @return bool|string
     */
    protected function get_password($pwd,$type=0)
    {
        return $type?substr($pwd, 10, 15):substr(md5($pwd), 10, 15);
    }

    /**
     * 检测验证码
     * @param string $verify
     * @param bool $clear
     * @param null $out
     * @return bool
     */
    protected function check_verify($verify='', $clear=false,&$out=null)
    {
        if(!$verify){
            $out= '请输入验证码';
            return false;
        }
        $d = cookie($verify.'_session_code');
        $f = cookie('?'.$verify.'_session_code');
        $out = $d;
        if (!$f) {
            $out = '验证码已失效';
            return false;
        }
        if ($verify!=$d) {
            $out = '验证码不正确';
            return false;
        }else{
            if ($clear) {
                cookie($verify.'_session_code', null);
            }
            $out = '验证码正确';
            return true;
        }
    }

    /**
     * 获取secret
     * @param int $length
     * @return string
     */
    protected function getSecret($length=6)
    {
        $random = new \Rych\Random\Random();
        $text = $random->getRandomString($length,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
        //$secret =  CryptoJSAES::encrypt($text, config('api.passphrase'));
        return $text;
    }

    /**
     * @param int $length
     * @return string
     */
    protected static function secret($length=32)
    {
        $random = new \Rych\Random\Random();
        $text = $random->getRandomString($length,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
        //$secret =  CryptoJSAES::encrypt($text, config('api.passphrase'));
        return $text;
    }

    /**
     * 参数规则
     * @name 字段名称
     * @type 类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    final static function getRules() {
        return [];
    }

    /**
     * @param null $user
     * @param string $alg
     * @param array $out
     * @return bool|string
     */
    final static function jwt($user=null,$alg = 'HS256',&$out=[]){
        if(!$user){
            return false;
        }
        if(empty($alg)){
            $alg = 'HS256';
        }
        $key = Config::get('app.ace_key');
        $payload['account'] = $user['account'];
        $payload['uid'] = $user['id'];  //(必填参数,值自定义)
        $payload['iat'] = $_SERVER['REQUEST_TIME'];//签发时间(必填参数)
        $payload['exp'] = $_SERVER['REQUEST_TIME'] +  Config::get('app.expire_time');//过期时间(必填参数)
        $out = $payload;
        $token = Jwt::encode($payload, $key ,$alg);
        return $token;
    }
}